#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#include "queued.h"

#define usage(arg) fprintf(stderr, QUEUE_PROJECT_NAME" "QUEUE_PEOJECT_VERSION"\n\n Usage: %s [Options]\n\n Which Options:\n"\
		"    -b\t--daemon\tstart run queued as a daemon.\n"\
		"      \t--start \tstart run queued as a daemon, same as daemon.\n"\
		"    -d\t--debug \tstart queued in debug mode.\n"\
		"    -e\t--stop  \tstop the runing daemon.\n"\
		"    -l\t--info  \tget queue status.\n\n"\
		"    -c\t--rqueue-start [rqueue type]\tcontinue rqueue.\n"\
		"    -k\t--rqueue-stop  [rqueue type]\tpause rqueue.\n\n"\
		"    -f\t--config-file  [path]\tset use defined config file.\n\n"\
		" [Rqueue types]:\n\t[HTTP|SHM|MSG|FIFO|DB|INNER|EXTER]\n\n"\
		, arg);

struct option longopts[] = {
	{ "daemon"        , no_argument      ,    NULL, 'b'},
	{ "start"         , no_argument      ,    NULL, 'b'},
	{ "stop"          , no_argument      ,    NULL, 'e'},
	{ "debug"         , no_argument      ,    NULL, 'd'},
	{ "info"          , no_argument      ,    NULL, 'l'},
	{ "config-file"   , required_argument,    NULL, 'f'},
	{ "rqueue-start"  , required_argument,    NULL, 'c'},
	{ "rqueue-stop"   , required_argument,    NULL, 'k'},
	{    0,    0,    0,    0}
};

int main(int argc, char ** argv)
{
	int c, break_out = 0;
	const char *config_file = NULL;
	const char *rqueue_type = NULL;

	if(NULL == root_path(argv[0])) {
		usage(argv[0]);
		return EXIT_SUCCESS;
	}

	while((c = getopt_long(argc, argv, "bedlf:c:k:", longopts, NULL)) != -1)
	{
		switch (c){
		case 'd':
		case 'b':
		case 'e':
		case 'l':
			break_out = 1;
			break;
		case 'f':
			config_file = optarg;
			break;
		case 'c':
		case 'k':
			break_out = 1;
			rqueue_type = optarg;
			break;
		case '?':
		default:
			usage(argv[0]);
			return EXIT_SUCCESS;
		}
		
		if(break_out == 1)
			break;
	}

	/* start as a daemon process. */
	if(c == 'b') {
		queue_init(config_file, 1, 0);
		queue_start();
		return EXIT_SUCCESS;
	}
	if(c == 'd') {
		queue_init(config_file, 1, 1);
		queue_start();
		return EXIT_SUCCESS;
	}	

	/* not daemon process, only read some configure. */
	queue_init(config_file, 0, 1);

	     if(c == 'e')
		command_pal(CMD_STOP);
	else if(c == 'f' && config_file != NULL)
		printf("config setting in progress.\n");
	else if(c == 'c' && rqueue_type != NULL)
	{
		     if(strcasecmp(rqueue_type, "HTTP" ) == 0)
			command_pal(CMD_START_HTTP);
		else if(strcasecmp(rqueue_type, "SHM"  ) == 0)
			command_pal(CMD_START_SHM);
		else if(strcasecmp(rqueue_type, "MSG"  ) == 0)
			command_pal(CMD_START_MSG);
		else if(strcasecmp(rqueue_type, "FIFO" ) == 0)
			command_pal(CMD_START_FIFO);
		else if(strcasecmp(rqueue_type, "DB"   ) == 0)
			command_pal(CMD_START_DB);
		else if(strcasecmp(rqueue_type, "INNER") == 0)
			command_pal(CMD_START_INNER);
		else if(strcasecmp(rqueue_type, "EXTER") == 0)
			command_pal(CMD_START_EXTER);
	}
	else if(c == 'k' && rqueue_type != NULL)
	{
		     if(strcasecmp(rqueue_type, "HTTP" ) == 0)
			command_pal(CMD_STOP_HTTP);
		else if(strcasecmp(rqueue_type, "SHM"  ) == 0)
			command_pal(CMD_STOP_SHM);
		else if(strcasecmp(rqueue_type, "MSG"  ) == 0)
			command_pal(CMD_STOP_MSG);
		else if(strcasecmp(rqueue_type, "FIFO" ) == 0)
			command_pal(CMD_STOP_FIFO);
		else if(strcasecmp(rqueue_type, "DB"   ) == 0)
			command_pal(CMD_STOP_DB);
		else if(strcasecmp(rqueue_type, "INNER") == 0)
			command_pal(CMD_STOP_INNER);
		else if(strcasecmp(rqueue_type, "EXTER") == 0)
			command_pal(CMD_STOP_EXTER);
	}
	else if(c == 'l')
		command_pal(CMD_INFO);
	else
		usage(argv[0]);

	queue_fini(0);
	return EXIT_SUCCESS;
}

